﻿using MesGLApp.BLL;
using MesGLApp.DAL.RoleDTO;
using Smobiler.Core;
using Smobiler.Core.Controls;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace MesGLApp.View.Search
{
    partial class AbnormalOrderFrm : Smobiler.Core.Controls.MobileForm
    {

        private SqlSugarClient Db = SqlSugares.GetInstance();

        BaseControler bs = new BaseControler();
        public AbnormalOrderFrm() : base()
        {
            //This call is required by the SmobilerForm.
            InitializeComponent();
            Select();
        }

        private void btnClose_Press(object sender, EventArgs e)
        {

        }

        private void btnScan_BarcodeScanned(object sender, BarcodeResultArgs e)
        {
            if (e.Value != null && e.Value.ToString().Length > 0)
            {
                this.txtProdNumber.Text = e.Value.ToString();
            }
        }
        /// <summary>
        /// 查询工单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Press(object sender, EventArgs e)
        {
            Select();
        }
        public void Select()
        {
            BaseUserDTO tmpUser = Client.Session["User"] as BaseUserDTO;
            List<int> deptIds = bs.SelectUserDept(tmpUser.id);
            string deptStr = "";
            for (int i = 0; i < deptIds.Count; i++)
            {
                deptStr += "'" + deptIds[i] + "'" + ",";
            }
            if (deptIds.Count != 0)
            {
                deptStr = deptStr.Substring(0, deptStr.Length - 1);
            }
            else
            {
                deptStr = "0";
            }
            var list = Db.Context.SqlQueryable<dynamic>($@"select
	            T1.id,
                T1.prod_number,
                T1.reason_name,
                T1.abnormal_qty,
                T1.status_name,
                T1.begin_time
            from
	            abnormal_detail T1 left join target_prod_process T2 on T1.prod_number = T2.prod_number
            where
                T1.prod_number like '%{txtProdNumber.Text}%' and end_time is null and T2.dept_id in ({deptStr}) and
                T1.begin_time >= '{datePicker1.Value.ToString("yyyy-MM-dd")}' and T1.begin_time <= '{datePicker2.Value.AddDays(1).ToString("yyyy-MM-dd")}'").ToList();

            DataTable table = new DataTable();
            table.Columns.Add("id", typeof(System.Int32));//id
            table.Columns.Add("ItemOrderNo", typeof(System.String));//工单号
            table.Columns.Add("Reason_name", typeof(System.String));//异常描述
            table.Columns.Add("abnormal_qty", typeof(System.Decimal));//异常数量
            table.Columns.Add("statue_name", typeof(System.String)); //异常状态
            table.Columns.Add("begin_time", typeof(System.DateTime));//异常时间
            for (int i = 0; i < list.Count; i++)
            {
                table.Rows.Add(list[i].id, list[i].prod_number, list[i].reason_name, list[i].abnormal_qty, list[i].status_name, list[i].begin_time);
            }
            listView1.Rows.Clear();     //清除数据
            if (table.Rows.Count > 0)    //绑定数据源
            {
                listView1.DataSource = table;
                listView1.DataBind();
            }
        }
    }
}